
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _utils = require('../utils');

const _constant = require('../utils/constant');

const _icon = _interopRequireDefault(require('../icon'));

// Utils
// Components
const _createNamespace = (0, _utils.createNamespace)('nav-bar');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];

const _default = createComponent({
  props: {
    title: String,
    fixed: Boolean,
    zIndex: [Number, String],
    leftText: String,
    rightText: String,
    leftArrow: Boolean,
    placeholder: Boolean,
    border: {
      type: Boolean,
      default: true,
    },
  },
  data: function data() {
    return {
      height: null,
    };
  },
  mounted: function mounted() {
    if (this.placeholder && this.fixed) {
      this.height = this.$refs.navBar.getBoundingClientRect().height;
    }
  },
  methods: {
    genLeft: function genLeft() {
      const h = this.$createElement;
      const leftSlot = this.slots('left');

      if (leftSlot) {
        return leftSlot;
      }

      return [this.leftArrow && h(_icon.default, {
        class: bem('arrow'),
        attrs: {
          name: 'arrow-left',
        },
      }), this.leftText && h('span', {
        class: bem('text'),
      }, [this.leftText])];
    },
    genRight: function genRight() {
      const h = this.$createElement;
      const rightSlot = this.slots('right');

      if (rightSlot) {
        return rightSlot;
      }

      if (this.rightText) {
        return h('span', {
          class: bem('text'),
        }, [this.rightText]);
      }
    },
    genNavBar: function genNavBar() {
      let _ref;

      const h = this.$createElement;
      return h('div', {
        ref: 'navBar',
        style: {
          zIndex: this.zIndex,
        },
        class: [bem({
          fixed: this.fixed,
        }), (_ref = {}, _ref[_constant.BORDER_BOTTOM] = this.border, _ref)],
      }, [h('div', {
        class: bem('left'),
        on: {
          click: this.onClickLeft,
        },
      }, [this.genLeft()]), h('div', {
        class: [bem('title'), 'van-ellipsis'],
      }, [this.slots('title') || this.title]), h('div', {
        class: bem('right'),
        on: {
          click: this.onClickRight,
        },
      }, [this.genRight()])]);
    },
    onClickLeft: function onClickLeft(event) {
      this.$emit('click-left', event);
    },
    onClickRight: function onClickRight(event) {
      this.$emit('click-right', event);
    },
  },
  render: function render() {
    const h = arguments[0];

    if (this.placeholder && this.fixed) {
      return h('div', {
        class: bem('placeholder'),
        style: {
          height: `${this.height}px`,
        },
      }, [this.genNavBar()]);
    }

    return this.genNavBar();
  },
});

exports.default = _default;
